package nix.bs.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nix.bs.domain.Book;
import nix.bs.domain.Category;
import nix.bs.service.BookService;
import nix.bs.service.CategoryService;
import nix.commons.CommonUtils;
import nix.servlet.BaseServlet;

public class AdminBookServlet extends BaseServlet {

	private BookService bookService = new BookService();
	private CategoryService categoryService = new CategoryService();

	/**
	 * 查询所有图书
	 */
	public String findAll(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		List<Book> bookList = bookService.findAll();
		request.setAttribute("bookList", bookList);
		return "f:/bs/adminjsps/admin/book/list.jsp";
	}

	/**
	 * 加载图书
	 */
	public String load(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setAttribute("book",
				bookService.load(request.getParameter("bid")));
		request.setAttribute("categoryList", categoryService.findAll());
		return "f:/bs/adminjsps/admin/book/desc.jsp";
	}
	
	/**
	 * 添加图书准备
	 */
	public String addPre(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setAttribute("categoryList", categoryService.findAll());
		return "f:/bs/adminjsps/admin/book/add.jsp";
	}

	/**
	 * 删除图书
	 */
	public String delete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Book book = CommonUtils.toBean(request.getParameterMap(), Book.class);
		bookService.delete(book.getBid());
		return findAll(request, response);
	}

	/**
	 * 修改图书
	 */
	public String edit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		Book book = CommonUtils.toBean(request.getParameterMap(), Book.class);
		Category category = CommonUtils.toBean(request.getParameterMap(), Category.class);
		book.setCategory(category);
		bookService.edit(book);
		return findAll(request, response);
	}
}
